﻿


// Author:					Doan Quoc Tuan
// Created:					2012-4-18
// Last Modified:			2012-4-18
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using SMSPanasonic.Data;

namespace SMSPanasonic.Business
{

    public class Logs
    {

        #region Constructors

        public Logs()
        { }


        public Logs(
            SqlInt64 logID)
        {
            GetLogs(
                logID);
        }

        #endregion

        #region Private Properties

        private SqlInt64 _logID = SqlInt64.Null;
        private SqlString _logType = SqlString.Null;
        private SqlString _logMessage = SqlString.Null;
        private SqlDateTime _logTime = SqlDateTime.Null;
        private SqlString _logTrace = SqlString.Null;
        private SqlString _logSource = SqlString.Null;
        private SqlBoolean _status = SqlBoolean.Null;
        private SqlInt32 _cooperateID = SqlInt32.Null;

        #endregion

        #region Public Properties

        public SqlInt64 LogID
        {
            get { return _logID; }
            set { _logID = value; }
        }
        public SqlString LogType
        {
            get { return _logType; }
            set { _logType = value; }
        }
        public SqlString LogMessage
        {
            get { return _logMessage; }
            set { _logMessage = value; }
        }
        public SqlDateTime LogTime
        {
            get { return _logTime; }
            set { _logTime = value; }
        }
        public SqlString LogTrace
        {
            get { return _logTrace; }
            set { _logTrace = value; }
        }
        public SqlString LogSource
        {
            get { return _logSource; }
            set { _logSource = value; }
        }
        public SqlBoolean Status
        {
            get { return _status; }
            set { _status = value; }
        }
        public SqlInt32 CooperateID
        {
            get { return _cooperateID; }
            set { _cooperateID = value; }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of Logs.
        /// </summary>
        /// <param name="logID"> logID </param>
        private void GetLogs(
            SqlInt64 logID)
        {
            using (IDataReader reader = DBLogs.GetOne(
                logID))
            {
                PopulateFromReader(reader);
            }

        }


        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                if (!reader.IsDBNull(0)) this._logID = reader.GetInt64(0);
                if (!reader.IsDBNull(1)) this._logType = reader.GetString(1);
                if (!reader.IsDBNull(2)) this._logMessage = reader.GetString(2);
                if (!reader.IsDBNull(3)) this._logTime = reader.GetDateTime(3);
                if (!reader.IsDBNull(4)) this._logTrace = reader.GetString(4);
                if (!reader.IsDBNull(5)) this._logSource = reader.GetString(5);
                if (!reader.IsDBNull(6)) this._status = reader.GetBoolean(6);
                if (!reader.IsDBNull(7)) this._cooperateID = reader.GetInt32(7);

            }

        }

        /// <summary>
        /// Persists a new instance of Logs. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create()
        {
            int newID = 0;

            newID = DBLogs.Create(
                this._logType,
                this._logMessage,
                this._logTime,
                this._logTrace,
                this._logSource,
                this._status,
                this._cooperateID);

            this._logID = newID;

            return (newID > 0);

        }


        /// <summary>
        /// Updates this instance of Logs. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {

            return DBLogs.Update(
                this._logID,
                this._logType,
                this._logMessage,
                this._logTime,
                this._logTrace,
                this._logSource,
                this._status,
                this._cooperateID);

        }





        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of Logs. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            if (!this._logID.IsNull)
            {
                return Update();
            }
            else
            {
                return Create();
            }

        }




        #endregion

        #region Static Methods

        /// <summary>
        /// Deletes an instance of Logs. Returns true on success.
        /// </summary>
        /// <param name="logID"> logID </param>
        /// <returns>bool</returns>
        public static bool Delete(
            SqlInt64 logID)
        {
            return DBLogs.Delete(
                logID);
        }


        /// <summary>
        /// Gets a count of Logs. 
        /// </summary>
        public static int GetCount(string sWhereCondition)
        {
            return DBLogs.GetCount(sWhereCondition);
        }

        private static List<Logs> LoadListFromReader(IDataReader reader)
        {
            List<Logs> _logsList = new List<Logs>();
            try
            {
                while (reader.Read())
                {
                    Logs _logs = new Logs();
                    if (!reader.IsDBNull(0)) _logs._logID = reader.GetInt64(0);

                    if (!reader.IsDBNull(1)) _logs._logType = reader.GetString(1);

                    if (!reader.IsDBNull(2)) _logs._logMessage = reader.GetString(2);

                    if (!reader.IsDBNull(3)) _logs._logTime = reader.GetDateTime(3);

                    if (!reader.IsDBNull(4)) _logs._logTrace = reader.GetString(4);

                    if (!reader.IsDBNull(5)) _logs._logSource = reader.GetString(5);

                    if (!reader.IsDBNull(6)) _logs._status = reader.GetBoolean(6);

                    if (!reader.IsDBNull(7)) _logs._cooperateID = reader.GetInt32(7);

                    _logsList.Add(_logs);

                }
            }
            finally
            {
                reader.Close();
            }

            return _logsList;

        }

        /// <summary>
        /// Gets an IList with all instances of Logs.
        /// </summary>
        public static List<Logs> GetAll()
        {
            IDataReader reader = DBLogs.GetAll();
            return LoadListFromReader(reader);

        }

        /// <summary>
        /// Gets an IList with page of instances of Logs.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<Logs> GetPage(string sWhereCondition, string sOrderBy, int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            IDataReader reader = DBLogs.GetPage(sWhereCondition, sOrderBy, pageNumber, pageSize, out totalPages);
            return LoadListFromReader(reader);
        }



        #endregion

        #region Comparison Methods

        /// <summary>
        /// Compares 2 instances of Logs.
        /// </summary>
        public static int CompareByLogType(Logs _logs1, Logs _logs2)
        {
            return _logs1.LogType.CompareTo(_logs2.LogType);
        }
        /// <summary>
        /// Compares 2 instances of Logs.
        /// </summary>
        public static int CompareByLogMessage(Logs _logs1, Logs _logs2)
        {
            return _logs1.LogMessage.CompareTo(_logs2.LogMessage);
        }
        /// <summary>
        /// Compares 2 instances of Logs.
        /// </summary>
        public static int CompareByLogTime(Logs _logs1, Logs _logs2)
        {
            return _logs1.LogTime.CompareTo(_logs2.LogTime);
        }
        /// <summary>
        /// Compares 2 instances of Logs.
        /// </summary>
        public static int CompareByLogTrace(Logs _logs1, Logs _logs2)
        {
            return _logs1.LogTrace.CompareTo(_logs2.LogTrace);
        }
        /// <summary>
        /// Compares 2 instances of Logs.
        /// </summary>
        public static int CompareByLogSource(Logs _logs1, Logs _logs2)
        {
            return _logs1.LogSource.CompareTo(_logs2.LogSource);
        }
        /// <summary>
        /// Compares 2 instances of Logs.
        /// </summary>
        public static int CompareByCooperateID(Logs _logs1, Logs _logs2)
        {
            return _logs1.CooperateID.CompareTo(_logs2.CooperateID);
        }

        #endregion


    }

}





